package com.peoit.honeycomb.controller;

import com.peoit.honeycomb.application.SecurityApplication;
import com.peoit.honeycomb.domain.security.Permission;
import com.peoit.honeycomb.domain.security.Role;
import com.peoit.honeycomb.dto.CommonResultJsonDto;
import com.peoit.honeycomb.dto.PageCommonResultDto;
import com.peoit.honeycomb.dto.security.PermissionDto;
import com.peoit.honeycomb.dto.security.PersonPageDto;
import com.peoit.honeycomb.dto.security.RoleDto;
import com.peoit.honeycomb.dto.security.RolePageDto;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 后台角色管理
 * Created by zhouqiu on 2015/10/17.
 */
@Controller
@RequestMapping("/admin/role")
public class RoleController extends HoneycombBaseController {

    @Autowired
    SecurityApplication securityApplication;


    @RequestMapping("/list/{offset}")
    public ModelAndView list(RolePageDto dto,@PathVariable int offset) {
        dto.setOffset(offset);
        List<RoleDto> dtos = securityApplication.findRolePageBy(dto);

        Long count = securityApplication.findRoleCount(dto);
        PageCommonResultDto pageCommonResultDto = new PageCommonResultDto();
        pageCommonResultDto.setResult(dtos);
        pageCommonResultDto.setPageSize(dto.getPageSize());
        pageCommonResultDto.setCount(count);
        pageCommonResultDto.setOffset(offset);

        return view("/jsp/system/role/rolelist",pageCommonResultDto);
    }



    @RequestMapping("/toRoleAdd")
    public ModelAndView toRoleAdd() {

        List<PermissionDto> permissionDtoList = securityApplication.findPermissionDtoList();
        return view("/jsp/system/role/roleadd",permissionDtoList);
    }

    @RequestMapping("/roleAdd")
    public @ResponseBody  CommonResultJsonDto roleAdd(RoleDto dto,String [] perId) {
        CommonResultJsonDto resultJsonDto = CommonResultJsonDto.success("成功！！");
        boolean b = this.securityApplication.roleCreateOrUpdate(dto, perId);
        if(b==false) {
            resultJsonDto = CommonResultJsonDto.fail("失败！！");
        }
        return resultJsonDto;
    }

    @RequestMapping("/toRoleUpdate/{id}")
    public ModelAndView toRoleUpdate(@PathVariable String id,ModelMap modelMap) {

        Role role = securityApplication.find(Role.class, id);
        RoleDto roleDto = new RoleDto();
        RoleDto roleDto1 = roleDto.toDto(role);
        List<PermissionDto> permissionDtoList = securityApplication.findPermissionDtoList();

        modelMap.addAttribute("roleDto1", roleDto1);
        return view("/jsp/system/role/roleedit",permissionDtoList);
    }


    @RequestMapping("/deleteRole")
    public @ResponseBody CommonResultJsonDto deleteRole( String id) {
        CommonResultJsonDto resultJsonDto = CommonResultJsonDto.success("删除成功！");
        try{
            this.securityApplication.deleteBy(Role.class, id);
        }catch (Exception c){
            resultJsonDto = CommonResultJsonDto.success("你已经分配给管理员人员,请解除关联后删除！");
        }
        return resultJsonDto;
    }
}
